package com.gameShare.service.misc

import com.gameShare.entity.dto.misc.MessageDto
import com.gameShare.enums.misc.ResponseCodeEnum
import com.gameShare.enums.user.UserActionEnum
import com.gameShare.exceptions.BusinessException
import com.gameShare.mappers.misc.GameCommentMapper
import com.gameShare.mappers.misc.MessageMapper
import com.gameShare.service.game.GameInfoService
import com.gameShare.service.user.AccountService
import com.gameShare.service.user.UserActionService
import org.springframework.stereotype.Service
import java.time.LocalDate

@Service
class MessageService(
    private val messageMapper: MessageMapper,
    private val accountService: AccountService,
    private val gameInfoService: GameInfoService,
    private val userActionService: UserActionService
) {


    /**
     * 读取消息列表
     *
     * @param userId 用户id
     * @param type 消息类型
     * @return
     */
    fun getMessageList(userId : String, type : Int?): List<MessageDto>? {
        return messageMapper.selectByUserIdAndType(userId, type)
    }

    /**
     * TODO admin 处理逻辑
     *
     * @param sendUserId
     * @param receiveUserId
     * @param gameId
     * @param messageType
     * @param content
     */
    fun sendMessage(sendUserId: String, receiveUserId : String, gameId : String,messageType : Int, content : String){
        // 检查发送者和接受者是否一致如果一致则啥都不干
        if (sendUserId == receiveUserId) return
        // 检查发送者接收者和游戏是否存在
        accountService.checkUserAccountExist(receiveUserId)
        accountService.checkUserAccountExist(sendUserId)
        gameInfoService.checkGameInfoExist(gameId)
        // 更新用户消息数量
        userActionService.updateUserActionCount(receiveUserId, UserActionEnum.MESSAGE.code, 1)
        // 将消息插入数据库
        messageMapper.insertMessage(sendUserId, receiveUserId, LocalDate.now(), gameId,messageType, content)
    }

    /**
     * 读消息
     *
     * @param userId 用户id
     * @param messageId 消息id
     * @return
     */
    fun readMessage(userId: String, messageId : Int): Boolean {
        checkMessageExist(userId, messageId = messageId)
        return messageMapper.updateMessageRead(userId, messageId) != 0
    }

    /**
     * 根据消息类型一键读消息
     *
     * @param userId 用户id
     * @param type 消息id
     * @return
     */
    fun readAllMessage(userId: String, type: Int): Boolean {
        checkMessageExist(userId, type = type)
        return messageMapper.updateMessageReadByType(userId, type) != 0
    }

    /**
     * 通过消息id删除消息
     *
     * @param userId 用户id
     * @param messageId
     * @return
     */
    fun delMessage(userId: String, messageId: Int) : Boolean{
        checkMessageExist(userId, messageId = messageId)
        return messageMapper.delMessageById(userId, messageId) != 0

    }

    /**
     * 通过消息类型一键删除消息
     *
     * @param userId 用户id
     * @param type 消息类型
     * @return
     */
    fun delAllMessage(userId: String, type: Int): Boolean {
        checkMessageExist(userId, type=type)
        return messageMapper.delMessageByType(userId, type) != 0
    }

    /**
     * 检查消息是否存在
     *
     * @param userId 用户id
     * @param type 消息类型
     * @param messageId 消息id
     * @throws BusinessException 业务异常 参数错误
     */
    private fun checkMessageExist(userId: String, type: Int? = null, messageId: Int? = null) {
        if (messageMapper.checkMessageExist(userId, type, messageId) != 1) {
            throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
        }
    }
}
